# -*-coding:utf-8-*-
import subprocess
import random
import urllib.request
from bs4 import BeautifulSoup
import Setting


def downNow(img_save):
    page = random.randint(1, 35)
    # page = 1

    url = 'https://bing.ioliu.cn/?p=%s' % page
    # 设置请求地址与请求参数

    request = urllib.request.Request(url=url, headers={'User-Agent': 'Mozilla/5.0'})
    # request.add_header('referer', 'http://www.weather.com.cn/weather1d/101120101.shtml')
    response = urllib.request.urlopen(request)
    # 读取请求结果并转换为JSON
    result = response.read()
    html = BeautifulSoup(result, 'lxml')
    html = html.select('.container')[1]
    html = html.select('.mark')
    html = html[random.randint(0, len(html) - 1)]
    img_name = html.get('href')
    img_name = img_name.replace('/photo/', '')
    img_name = img_name[:img_name.rfind('?')]
    img_href = 'https://bing.ioliu.cn/photo/%s?force=download' % img_name


    # 下载图片
    comm = '/usr/bin/curl %s %s --output %s' % (Setting.proxy, img_href, img_save)
    subprocess.run(comm,shell=True)
    return img_save
